{% extends "layout/content.twig" %}

{% block main %}
    <div class="layui-card">
        <div class="layui-card-header">
            {{ __('backup.title') }}
        </div>

        <div class="layui-card-body">
            <div class="layui-tab layui-tab-brief">
                <ul class="layui-tab-title">
                    <li class="layui-this">{{ __('backup.log') }}</li>
                    <li class="">{{ __('backup.manual') }}</li>
                    <li class="">{{ __('backup.setting') }}</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        {% include "database/log.twig" %}
                    </div>
                    <div class="layui-tab-item ">
                        {% include "database/tables.twig" %}
                    </div>
                    <div class="layui-tab-item ">
                        {% include "database/setting.twig" %}
                    </div>
                </div>
            </div>


        </div>
    </div>

{% endblock %}

{% block script %}
    <script type="text/javascript">

      let save_config_url = '{{ auth_route('general/saveSetting') }}'

      layui.use(['http', 'toast', 'form', 'upload', 'table'], function () {

        let http = layui.http
        let form = layui.form
        let toast = layui.toast
        let table = layui.table
        let layer = layui.layer

        layui.code({
          elem: '.cron-code',
        })

        form.on('submit(database)', function (d) {
          let fields = d.field
          http.post(save_config_url,
            {
              'group': 'backup',
              setting: fields,
            }, function (res) {
              toast.response(res)
            })
          return false
        })

        // 设置备份的表（废弃，修改为配置文件中配置）
        form.on('submit(tables)', function (d) {
          let fields = d.field
          let tables = []
          for (let i in fields) {
            tables.push(i)
          }

          http.post(save_config_url,
            {
              'group': 'database',
              setting: {
                tables: tables,
              },
            }, function (res) {
              toast.response(res)
            })
          return false
        })

        // 立即备份
        let command = '{{ see_config('database')['command'] }}'
        if (command) {
          command = '{{ __('database.bk_command') }}<br/>' + command
        }
        form.on('submit(backup-now)', function (d) {
          let fields = d.field
          let tables = []
          for (let i in fields) {
            tables.push(i)
          }

          if (tables.length === 0) {
            toast.top('Please select a table')
            return false
          }

          layer.confirm('{{ __('database.bk_confirm_content') }}<br/>' + command, {
              title: '{{ __('database.bk_confirm_title') }}',
              icon: 3,
              offset: '180px',
              btn: ['{{ __('btn.ok') }}', '{{ __('btn.cancel') }}'],
            },
            function () {
              let loading = layer.open({
                type: 3,
                icon: 2,
                shade: 0.4,
              })
              setTimeout(function () {
                layer.close(loading)
              }, 3000)
              http.post('{{ auth_route('database/backup') }}', { tables: tables }, function (res) {
                layer.close(loading)
                toast.response(res)
              })
            })
        })

        let options = table_common_options('{{ auth_route('database/index') }}', [
          { field: 'id', width: 80, title: 'ID' },
          { field: 'name', width: 120, title: '{{ __('database.name') }}' },
          { field: 'begin_id', width: 80, title: '{{ __('database.begin_id') }}' },
          { field: 'end_id', width: 80, title: '{{ __('database.end_id') }}' },
          { field: 'increase', width: 80, title: '{{ __('database.increase') }}' },
          { field: 'filename', title: '{{ __('database.filename') }}' },
          { field: 'filesize_text', width: 100, title: '{{ __('database.filesize') }}' },
          { field: 'send_at', width: 180, title: '{{ __('database.send_at') }}' },
          { field: 'send_result_text', title: '{{ __('database.send_result') }}' },
          { field: 'send_fail_reason', title: '{{ __('database.send_fail_reason') }}' },
          { field: 'created_at', title: '{{ __('created_at') }}' },
          {
            field: 'action', title: '{{ __('btn.operation') }}', templet: function (d) {
              return `
                  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="download" >{{ __('btn.download') }}</a>
                  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="send"">{{ __('btn.send') }}</a>
                  `
            }
          },
        ])

        table.render(options)

        table.on('tool(backup-log)', function (obj) {
          let data = obj.data
          let event = obj.event
          if (event === 'download') {
            let index = toast.confirm('{{ __('database.confirm_download') }}', function () {
              window.open('{{ auth_route('database/download') }}?id=' + data.id)
              layer.close(index)
            })
          } else if (event === 'send') {
            let index = toast.confirm('{{ __('database.confirm_email') }}', function () {
              http.post('{{ auth_route('database/send') }}', { id: data.id }, function (res) {
                toast.response(res)
                layer.close(index)
              })

            })
          }
        })

        table.on('toolbar(backup-log)', function (obj) {
          if (obj.event === '__reload__') {
            table.reload('page-table-render', {}, true)
          }
        })

        form.on('submit(search)', function (data) {
          table.reloadData('page-table-render', {
            where: data.field,
          })
          return false
        })
      })
    </script>
{% endblock %}